/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * StatWindow.java
 *
 * Created on 26.5.2009, 11:52:20
 */
package semestralka_avt;

import javax.swing.JOptionPane;

/**
 *
 * @author FReSH
 */
public class StatWindow extends javax.swing.JFrame {

    /** Creates new form StatWindow */
    public StatWindow() {
        initComponents();
        Slovo_Cizi.setEditable(false);
        Slovo_CZ.setEditable(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jOptionPane1 = new javax.swing.JOptionPane();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        Slovo_Cizi = new javax.swing.JTextField();
        Jazyk = new javax.swing.JComboBox();
        Slovo_CZ = new javax.swing.JTextField();
        Kniha = new javax.swing.JComboBox();
        Lekce = new javax.swing.JComboBox();
        Slovo_cz_enabled = new javax.swing.JCheckBox();
        Slovo_cizi_enabled = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Statistiky 1.0");
        setLocationByPlatform(true);

        jButton2.setText("Zobraz");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Storno");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel3.setText("Jazyk:");

        jLabel1.setText("Konkretni slovicko CZ:");

        jLabel2.setText("Konkretni slovicko cizi:");

        jLabel4.setText("Kniha:");

        jLabel5.setText("Lekce:");

        Jazyk.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "sp" }));
        Jazyk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JazykActionPerformed(evt);
            }
        });

        Kniha.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "All", "Buena" }));

        Lekce.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "All", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15" }));

        Slovo_cz_enabled.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                Slovo_cz_enabledStateChanged(evt);
            }
        });

        Slovo_cizi_enabled.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                Slovo_cizi_enabledStateChanged(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(Lekce, 0, 126, Short.MAX_VALUE)
                            .addComponent(Jazyk, 0, 126, Short.MAX_VALUE)
                            .addComponent(Kniha, javax.swing.GroupLayout.Alignment.TRAILING, 0, 126, Short.MAX_VALUE)
                            .addComponent(Slovo_Cizi, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(Slovo_CZ, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(Slovo_cz_enabled, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(Slovo_cizi_enabled, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(Jazyk, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(Slovo_CZ, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Slovo_cz_enabled))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(Slovo_Cizi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(Kniha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(Lekce, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(Slovo_cizi_enabled))
                .addGap(80, 80, 80)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE)
                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        String dotaz = new String();
        int slov = 0;
        int dobre = 0;
        int celkem = 0;
        double procent = 0;

        if (Slovo_cz_enabled.isSelected()) {
            dotaz = "SELECT SUM(dobre) FROM " + (String) Jazyk.getSelectedItem() + " WHERE slovo_cz = '" + Slovo_CZ.getText() + "'";
            dobre = Database.stat(dotaz);
            dotaz = "SELECT SUM(zkouseno) FROM " + (String) Jazyk.getSelectedItem() + " WHERE slovo_cz = '" + Slovo_CZ.getText() + "'";
            celkem = Database.stat(dotaz);
            if (dobre != 0) {
                procent = dobre / celkem * 100;
            }
            JOptionPane.showMessageDialog(null, "Zadne slovo '" + Slovo_CZ.getText() + "' bylo celkem zkouseno " + celkem + "x a z toho bylo " + dobre + " spravnych odpovedi = " + procent + "% uspesnost");
        }
        if (Slovo_cizi_enabled.isSelected()) {
            dotaz = "SELECT SUM(dobre) FROM " + (String) Jazyk.getSelectedItem() + " WHERE slovo_cizi = '" + Slovo_Cizi.getText() + "'";
            dobre = Database.stat(dotaz);
            dotaz = "SELECT SUM(zkouseno) FROM " + (String) Jazyk.getSelectedItem() + " WHERE slovo_cizi = '" + Slovo_Cizi.getText() + "'";
            celkem = Database.stat(dotaz);
            if (dobre != 0) {
                procent = dobre / celkem * 100;
            }
            JOptionPane.showMessageDialog(null, "Zadne slovo '" + Slovo_Cizi.getText() + "' bylo celkem zkouseno " + celkem + "x a z toho bylo " + dobre + " spravnych odpovedi = " + procent + "% uspesnost");
        }

        if (!Slovo_cizi_enabled.isSelected() && !Slovo_cz_enabled.isSelected()) {
            if (Kniha.getSelectedIndex() != 0) {
                if (Lekce.getSelectedIndex() != 0) {
                    dotaz = "SELECT COUNT(ID) FROM " + (String) Jazyk.getSelectedItem() + " WHERE kniha = '" + (String) Kniha.getSelectedItem() + "' AND lekce = " + (String) Lekce.getSelectedItem();
                    slov = Database.stat(dotaz);
                    dotaz = "SELECT SUM(dobre) FROM " + (String) Jazyk.getSelectedItem() + " WHERE kniha = '" + (String) Kniha.getSelectedItem() + "' AND lekce = " + (String) Lekce.getSelectedItem();
                    dobre = Database.stat(dotaz);
                    dotaz = "SELECT SUM(zkouseno) FROM " + (String) Jazyk.getSelectedItem() + " WHERE kniha = '" + (String) Kniha.getSelectedItem() + "' AND lekce = " + (String) Lekce.getSelectedItem();
                    celkem = Database.stat(dotaz);
                    if (dobre != 0) {
                        procent = dobre / celkem * 100;
                    }
                    JOptionPane.showMessageDialog(null, "V databazi pro knihu " + (String) Kniha.getSelectedItem() + " v lekci " + (String) Lekce.getSelectedItem() + " je celkem " + slov + " slov, vsechny byly zkouseny celkem " + celkem + "x a z toho bylo " + dobre + " spravnych odpovedi = " + procent + "% uspesnost");
                } else {
                    dotaz = "SELECT COUNT(ID) FROM " + (String) Jazyk.getSelectedItem() + " WHERE kniha = '" + Kniha.getSelectedItem() + "'";
                    slov = Database.stat(dotaz);
                    dotaz = "SELECT SUM(dobre) FROM " + (String) Jazyk.getSelectedItem() + " WHERE kniha = '" + Kniha.getSelectedItem() + "'";
                    dobre = Database.stat(dotaz);
                    dotaz = "SELECT SUM(zkouseno) FROM " + (String) Jazyk.getSelectedItem() + " WHERE kniha = '" + Kniha.getSelectedItem() + "'";
                    celkem = Database.stat(dotaz);
                    if (dobre != 0) {
                        procent = dobre / celkem * 100;
                    }
                    JOptionPane.showMessageDialog(null, "V databazi pro knihu " + (String) Kniha.getSelectedItem() + " je celkem " + slov + " slov, vsechny byly zkouseny celkem " + celkem + "x a z toho bylo " + dobre + " spravnych odpovedi = " + procent + "% uspesnost");
                }
            } else {
                dotaz = "SELECT COUNT(ID) FROM " + (String) Jazyk.getSelectedItem();
                slov = Database.stat(dotaz);
                dotaz = "SELECT SUM(dobre) FROM " + (String) Jazyk.getSelectedItem();
                dobre = Database.stat(dotaz);
                dotaz = "SELECT SUM(zkouseno) FROM " + (String) Jazyk.getSelectedItem();
                celkem = Database.stat(dotaz);
                if (dobre != 0) {
                    procent = dobre / celkem * 100;
                }
                JOptionPane.showMessageDialog(null, "V databazi je celkem " + slov + " slov, vsechny byly zkouseny celkem " + celkem + "x a z toho bylo " + dobre + "spravnych odpovedi = " + procent + "% uspesnost");
            }
        }
}//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void JazykActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JazykActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_JazykActionPerformed

    private void Slovo_cizi_enabledStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_Slovo_cizi_enabledStateChanged
        if (Slovo_cizi_enabled.isSelected()) {
            Slovo_Cizi.setEditable(true);
            Slovo_CZ.setEditable(false);
            Slovo_cz_enabled.setSelected(false);
        } else {
            Slovo_Cizi.setEditable(false);
        }
    }//GEN-LAST:event_Slovo_cizi_enabledStateChanged

    private void Slovo_cz_enabledStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_Slovo_cz_enabledStateChanged
        if (Slovo_cz_enabled.isSelected()) {
            Slovo_CZ.setEditable(true);
            Slovo_Cizi.setEditable(false);
            Slovo_cizi_enabled.setSelected(false);
        } else {
            Slovo_CZ.setEditable(false);
        }
    }//GEN-LAST:event_Slovo_cz_enabledStateChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new StatWindow().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox Jazyk;
    private javax.swing.JComboBox Kniha;
    private javax.swing.JComboBox Lekce;
    private javax.swing.JTextField Slovo_CZ;
    private javax.swing.JTextField Slovo_Cizi;
    private javax.swing.JCheckBox Slovo_cizi_enabled;
    private javax.swing.JCheckBox Slovo_cz_enabled;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JOptionPane jOptionPane1;
    // End of variables declaration//GEN-END:variables
}
